package com.cysoft.dao;

import com.cysoft.domain.Inventory;
import com.cysoft.domain.Mold;
import com.cysoft.domain.Task;
import com.cysoft.domain.Wheel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author andaolong
 * @time 2021/3/27-15:06
 * @describe
 */
@Component
public class TaskDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    WheelDao wheelDao;

    @Autowired
    MoldDao moldDao;

    Long taskPlanningId = 10L;

    //查询所有的device的id，返回device类型的list，里面还未填入machiningType
    public List<Task> getBomTaskListFromDB() {

        //首先查询到所有的bom节点，还没传入Process
        String sql = "select * from deliver where wheel_code not like '%D%'";
        String sql2 = "select * from inventory";
        //将查询到的所有bomTask存到一串<Task>的list里面
        //return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Task.class));

        List<Inventory> inventoryList = jdbcTemplate.query(sql2, new BeanPropertyRowMapper<Inventory>(Inventory.class));

        //给从数据库中获得的bomTask添加上id，排程的时候需要依靠这个id进行移动操作
        //因为这个id只能通过构造函数传入，所以，这里先把所有属性取出来再加上id通过构造函数新建一个对象，然后返回新对象
        List<Task> newTaskList = new ArrayList<>();

        List<Task> bomTaskList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Task>(Task.class){
            @Override
            public Task mapRow(ResultSet rs, int rowNumber) throws SQLException {
                String taskName = rs.getString("wheel_code")  ;
                String wheelCode = rs.getString("wheel_code");

                Task task = new Task(rs.getInt("id"), rs.getInt("order_id"),taskName,
                        wheelCode,rs.getInt("order_priority"),rs.getString("customer_name"), rs.getDate("payment_time").toLocalDate().atTime(0,0)
                ,rs.getInt("count"),rs.getInt("payment_type"));

                Wheel wheel = wheelDao.getWheelByCode(wheelCode);
                if(wheel == null) {
                    throw new NullPointerException("wheel query null:"+wheelCode);
                }
                task.setWheel(wheel);

                // 初始先分配一个
                List<Mold> molds = moldDao.getMoldListByWheelCode(wheelCode);
                if(molds != null && !molds.isEmpty()) {
                    task.setMold(molds.get(0));
                }
                // 减库存
                reduceInventory(task,inventoryList);
                if(task.getCount() > 0) {
                    newTaskList.add(task);
                }
                return task;
            }
        });


        return newTaskList;
    }

    void reduceInventory(Task task, List<Inventory> inventoryList) {
        for (Inventory inventory:inventoryList) {
            if(task.getWheelCode().equals(inventory.getWheelCode())) {
                if(task.getCount() >= inventory.getInventory()) {
                    task.setCount(task.getCount() - inventory.getInventory());
                    inventory.setInventory(0);
                } else {
                    task.setCount(0);
                    inventory.setInventory(inventory.getInventory() - task.getCount());
                }
                break;
            }
        }
    }

}
